package com.zerowidth.db.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间戳转换工具类
 * 注：所有方法入参都是时间戳，时间戳的单位统一使用 毫秒
 * Created by meikai on 2019/07/01.
 */
public class TimeUtils {

    public static final String TIME_FORMAT_YYYY_MM_DD_1 = "yyyy.MM.dd";
    public static final String TIME_FORMAT_YYYY_MM_DD_2 = "yyyy-MM-dd";

    public static final String TIME_FORMAT_MM_DD_1 = "MM-dd";


    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_2 = "yyyy年MM月dd日 HH:mm:ss";

    public static final String TIME_FORMAT_MM_DD_HH_MM = "MM月dd日 HH:mm";

    public static final String TIME_FORMAT_HH_MM = "HH:mm";
    public static final String TIME_FORMAT_HH_MM_SS = "HH:mm:ss";

    /**
     * @param timeStamp 时间戳，单位：毫秒
     * @param format    转换的格式，形如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String convert(long timeStamp, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(new Date(timeStamp));

        } catch (Exception e) {
        }
        return "";
    }

    public static long convert(String timeStampStr, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(timeStampStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getTodayStart() {
        return convert(System.currentTimeMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getTodayEnd() {
        return convert(System.currentTimeMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 23:59:59";
    }

    public static String getYesterdayStart() {
        long timeStamp = System.currentTimeMillis();
        timeStamp -= 24 * 60 * 60 * 1000;
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getYesterdayEnd() {
        long timeStamp = System.currentTimeMillis();
        timeStamp -= 24 * 60 * 60 * 1000;
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2) + " 23:59:59";
    }

    public static String getMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getYearStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getYearEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastYearStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastYearEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String convertYYYYMMDD(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_1);
    }

    public static String convertYYYYMMDD2(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2);
    }

    /**
     * 获取 24小时制下的  HH:mm
     */
    public static String convertHHMM_24(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_HH_MM);
    }

    /**
     * 获取 12小时制下的  HH:mm
     */
    public static String convertHHMM_12(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);

        int hour12JDK = calendar.get(Calendar.HOUR); //jdk 12小时制的 hour值域=[0-11]
        int hour12ui = hour12JDK;  //但在ui显示上，jdk hour=0应该显示为 12
        if (hour12JDK == 0) {
            hour12ui = 12;
        }

        return hour12ui + ":" + String.format("%02d", calendar.get(Calendar.MINUTE));
    }

    public static String convertMMDDHHMM(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_MM_DD_HH_MM);
    }

    /**
     * 判断时间是否为本周(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return currentYear == paramYear && paramWeek == currentWeek;
    }

    /**
     * 判断时间是否为下周(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isNextWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return currentYear == paramYear && paramWeek == currentWeek + 1;
    }

    /**
     * 判断时间是否为今年(以当前手机的时间为参照)
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisYear(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        return currentYear == paramYear;
    }

}